<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-09
 * Time: 16:01
 */

namespace app\service;


use app\common\model\DynamicComment;
use app\common\model\GuideArticle;
use app\common\model\GuideComment;
use app\common\model\GuideThumb;
use think\Db;
use think\Exception;
use think\Model;

/**
 * 导游动态评论
 * Class GuideCommentService
 * @package app\service
 */
class DynamicCommentService extends BaseService
{
    public $dynamicCommentModel;

    public function __construct()
    {
        $this->dynamicCommentModel = new DynamicComment();
    }


    /**
     * 新增/编辑
     * @param $add
     * @param null $id
     * @return false|int
     */
    public function add($data, $where = [])
    {
        if (!$where) {
            return $this->dynamicCommentModel->insertGetId($data);
        } else {
            return $this->dynamicCommentModel->save($data, $where);
        }
    }

    /**
     * 分页查询
     * @param $where
     * @param int $page
     * @param int $size
     * @return array|bool
     */
    public function queryPage($where, $page = 1, $size = 20, $field = "a.*,b.nickname,b.headimgurl", $order = "a.create_time desc")
    {
        try {
            $data = $this->dynamicCommentModel
                ->alias('a')
                ->join('zkhb_user b', 'a.uid=b.id', 'left')
                ->field($field)
                ->where($where)
                ->page($page, $size)
                ->order($order)
                ->select()
                ->toArray();
            foreach ($data as $key => $val) {
                if ($val['headimgurl']) {
                    $data[$key]['headimgurl'] = tomedia($val['headimgurl']);
                }
            }
            return $data;
        } catch (Exception $ex) {
            LogService::S()->writeLog('Exception', $ex->getMessage());
            return false;
        }
    }


    /**
     * 总条数
     * @param $where
     * @return bool|int|string
     */
    public function queryCount($where)
    {
        try {
            $count = $this->dynamicCommentModel
                ->alias('a')
                ->join('zkhb_user b', 'a.uid=b.id', 'left')
                ->where($where)
                ->count();
            return $count ? $count : 0;
        } catch (Exception $ex) {
            LogService::S()->writeLog('Exception', $ex->getMessage());
            return false;
        }
    }

}